package com.hfh.set;

import java.util.Objects;

public class Teacher implements Comparable<Teacher> {
    private String name;
    private Integer age;

    public Teacher() {
    }

    public Teacher(String name) {
        this.name = name;
    }

    public Teacher(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    /*
    添加name、age属性；
    重写compareTo方法，实现name、age升序；创建对象添加到TreeSet，观察结果。

    修改上题，实现name、age倒序；name升序，age倒序；name倒序、age升序。

     */
    @Override
    public int compareTo(Teacher o) {
        int i = -name.compareTo(o.name);

        if (i == 0) {// name值相同
            // 比较age
            return age.compareTo(o.getAge());
        } else {
            return i;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Teacher teacher = (Teacher) o;
        return Objects.equals(name, teacher.name) && Objects.equals(age, teacher.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

